namespace Rob
{
	namespace UI
	{
		class TimeWidget : Rob.UI.RuleWidget
		{
			private Rob.Rules.Time _time;
			private Gtk.Frame main_frame;
			private Gtk.VBox main_vbox;
			private Gtk.SpinButton start_mins; 
			private Gtk.SpinButton start_hs; 
			private Gtk.SpinButton end_mins; 
			private Gtk.SpinButton end_hs; 

			private Gtk.CheckButton monday;
			private Gtk.CheckButton tuesday;
			private Gtk.CheckButton wednesday;
			private Gtk.CheckButton thursday;
			private Gtk.CheckButton friday;
			private Gtk.CheckButton saturday;
			private Gtk.CheckButton sunday;

			construct 
			{
				this.main_frame = new Gtk.Frame("");
				this.add(this.main_frame);
				create_widgets();

			}


			private void create_widgets()
			{
				add_timewidgets();
				add_daywidgets();	
			}
			private void add_timewidgets()
			{
				Gtk.HBox tmp = new Gtk.HBox(false, 5);	
				this.start_mins = new Gtk.SpinButton(new Gtk.Adjustment( 0.0, 0.0, 60.0, 1.0, 5.0, 2 ),1.0,2);
				this.start_mins.value_changed += spin =>
				{
					this._time.starttime = spin.value + this.start_hs.value * 60;
				};
				this.main_vbox.pack_start_defaults( tmp);
				tmp.pack_start_defaults(new Gtk.Label("Start:"));
				tmp.pack_start_defaults(this.start_mins);
				this.start_hs = new Gtk.SpinButton(new Gtk.Adjustment( 0.0, 0.0, 24.0, 1.0, 5.0, 2 ),1.0,2);
				this.start_hs.value_changed += spin =>
				{
					this._time.starttime = spin.value * 60 + this.start_mins.value;
				};
				tmp.pack_start_defaults(this.start_hs);
				tmp = new Gtk.HBox(false, 5);	
				this.end_mins = new Gtk.SpinButton(new Gtk.Adjustment( 0.0, 0.0, 60.0, 1.0, 5.0, 2 ),1.0,2);
				this.end_mins.value_changed += spin =>
				{
					this._time.endtime = spin.value + this.end_hs.value * 60;
				};
				this.main_vbox.pack_start_defaults( tmp);
				tmp.pack_start_defaults(new Gtk.Label("End:"));
				tmp.pack_start_defaults(this.end_mins);
				this.end_hs = new Gtk.SpinButton(new Gtk.Adjustment( 0.0, 0.0, 24.0, 1.0, 5.0, 2 ),1.0,2);
				this.end_hs.value_changed += spin =>
				{
					this._time.endtime = spin.value * 60 + this.end_mins.value;
				};
				tmp.pack_start_defaults(this.end_hs);
			}

			private void add_daywidgets()
			{
				
				this.monday = new Gtk.CheckButton.with_label("Monday");
				this.main_vbox.pack_start_defaults(this.monday);
				this.monday.toggled += ckb =>
				{
					this._time.set_day(GLib.DateWeekday.MONDAY, ckb.active);	
				};

				this.tuesday = new Gtk.CheckButton.with_label("Tuesday");
				this.main_vbox.pack_start_defaults(this.tuesday);

				this.wednesday = new Gtk.CheckButton.with_label("Wednesdayday");
				this.main_vbox.pack_start_defaults(this.wednesday);

				this.thursday = new Gtk.CheckButton.with_label("Thursday");
				this.main_vbox.pack_start_defaults(this.thursday);

				this.friday = new Gtk.CheckButton.with_label("Friday");
				this.main_vbox.pack_start_defaults(this.friday);

				this.saturday = new Gtk.CheckButton.with_label("Saturday");
				this.main_vbox.pack_start_defaults(this.saturday);

				this.sunday = new Gtk.CheckButton.with_label("Sunday");
				this.main_vbox.pack_start_defaults(this.sunday);
			}

			private void update_values()
			{
				this.monday.active = this._time.get_day(GLib.DateWeekday.MONDAY );
				this.tuesday.active = this._time.get_day(GLib.DateWeekday.TUESDAY );
				this.wednesday.active = this._time.get_day(GLib.DateWeekday.WEDNESDAY );
				this.thursday.active = this._time.get_day(GLib.DateWeekday.THURSDAY );
				this.friday.active = this._time.get_day(GLib.DateWeekday.FRIDAY );
				this.saturday.active = this._time.get_day(GLib.DateWeekday.SATURDAY );
				this.sunday.active = this._time.get_day(GLib.DateWeekday.SUNDAY );
				this.end_mins.set_value(this._time.endtime%60);
				this.end_hs.set_value(this._time.endtime/60);
				this.start_mins.set_value(this._time.endtime%60);
				this.start_hs.set_value(this._time.endtime/60);
				this.main_frame.show_all();
			}

			public /*override*/ void set_rule(Rob.Rules.Rule r)
			{
				this._time = (Rob.Rules.Time) r ;
				update_values();
			}
			public /*override*/ Rob.Rules.Rule get_rule()
			{ 
				return this._time; 
			}
		}

		/*public class MinuteSpin: Gtk.SpinButton
		{
			private int time;
			public override signal void changed()
			{

			}
		}*/
	}
}
